package com.unitedvision.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityNotFoundException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unitedvision.core.domain.Operator;
import com.unitedvision.core.exception.ApplicationException;
import com.unitedvision.core.exception.NotAuthenticatedException;
import com.unitedvision.core.exception.WrongDependencyException;
import com.unitedvision.persistence.domain.CompanyEntity;
import com.unitedvision.persistence.domain.OperatorEntity;
import com.unitedvision.persistence.repository.CompanyRepository;
import com.unitedvision.persistence.repository.OperatorRepository;
import com.unitedvision.service.Authenticator;
import com.unitedvision.service.OperatorService;

@Service
public class OperatorServiceImpl implements OperatorService, Authenticator {

	@Resource
	private OperatorRepository operatorRepository;
	@Resource
	private CompanyRepository companyRepository;
	
	@Override
	@Transactional
	public void save(OperatorEntity operatorEntity) throws ApplicationException {
		if (!(operatorEntity.getCompany().isActive()))
			throw new ApplicationException("Company berstatus NON-AKTIF");
		
		operatorRepository.save(operatorEntity);
	}

	@Override
	@Transactional
	public void delete(OperatorEntity operatorEntity) {
		operatorRepository.delete(operatorEntity);
	}

	@Override
	public OperatorEntity get(int id) {
		return operatorRepository.findOne(id);
	}

	@Override
	public List<OperatorEntity> getAll() {
		return operatorRepository.findAll();
	}

	@Override
	public List<OperatorEntity> getByCompany(CompanyEntity companyEntity) {
		return operatorRepository.findByCompany(companyEntity);
	}

	@Override
	public List<OperatorEntity> getByCompanyAndName(CompanyEntity company, String name) throws EntityNotFoundException, WrongDependencyException {
		if (company instanceof CompanyEntity)
			return operatorRepository.findByCompanyAndNameContaining(company, name);
		
		throw new WrongDependencyException("company variable must be object of class CompanyEntity");
	}

	@Override
	public OperatorEntity getOneByCompanyAndName(CompanyEntity company, String name) throws EntityNotFoundException, WrongDependencyException {
		if (company instanceof CompanyEntity)
			return operatorRepository.findByCompanyAndName(company, name);
		
		throw new WrongDependencyException("company variable must be object of class CompanyEntity");
	}

	@Override
	public Operator authenticate(String username, String password) throws NotAuthenticatedException {
		try {
			OperatorEntity op = operatorRepository.findByUsername(username);
			
			if (op.isAuthenticated(password))
				return op.toOperator();

			throw new NotAuthenticatedException("Username atau Password salah");
		} catch (NullPointerException ex) {
			throw new NotAuthenticatedException("Username atau Password salah");
		}
	}

	@Override
	public long count() {
		return operatorRepository.count();
	}
}
